/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package forms;

import com.sun.lwuit.Button;
import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Graphics;
import com.sun.lwuit.Image;
import com.sun.lwuit.Label;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.geom.Dimension;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.FlowLayout;
import view.img.ImageStore;



/**
 *
 * @author victorinox
 */
public class WaitForm extends AbstractForm {

    private String string;
    private Thread t;
    private static WaitForm instance = new WaitForm();

    public static WaitForm getInstance() {
        return instance;
    }
   
    private WaitForm() {
        super("Aguarde...");
        setString("Aguarde...");
        setLayout(new BorderLayout());
    }

    protected void init() {

        if (getString() != null || getString().trim().length() > 0) {
            super.addComponent(BorderLayout.NORTH, new Label(getString()));
        }
        Image a = ImageStore.getInstance().getImage("progress0.png");
        Image b = ImageStore.getInstance().getImage("progress1.png");
        Image c = ImageStore.getInstance().getImage("progress2.png");
        final Image[] secondAnimation = new Image[]{
            a,
            b,
            c,
            a.rotate(90),
            b.rotate(90),
            c.rotate(90),
            a.rotate(180),
            b.rotate(180),
            c.rotate(180),
            a.rotate(270),
            b.rotate(270),
            c.rotate(270),
        };

        Button animation2 = new Button() {

            private int currentImage = 0;
            private long lastInvoke;

            public void initComponent() {
                registerAnimated(this);
            }

            public boolean animate() {
                long current = System.currentTimeMillis();
                if (current - lastInvoke > 50) {
                    lastInvoke = current;
                    currentImage++;
                    if (currentImage == secondAnimation.length) {
                        currentImage = 0;
                    }
                    return true;
                }
                return false;
            }

            public void paint(Graphics g) {
                g.drawImage(secondAnimation[currentImage], getX(), getY());
            }
        };

        animation2.setPreferredSize(new Dimension(secondAnimation[0].getWidth(), secondAnimation[0].getHeight()));
        animation2.setBorderPainted(false);
        animation2.getStyle().setBgTransparency(0);
        Container cont = new Container(new FlowLayout(Component.CENTER));
        cont.addComponent(animation2);
        addComponent(BorderLayout.NORTH, cont);

        /*Container cont1 = new Container(new FlowLayout(Component.CENTER));
        cont1.addComponent(new Label(ImageStore.getInstance().getImage("logo")));
        addComponent(BorderLayout.CENTER, cont1);*/

        if (this.t != null) {
            addCommand(new Command("Cancelar") {

                public void actionPerformed(ActionEvent arg0) {
                    //MObjectController.stopSearch();
                    t.setPriority(Thread.MIN_PRIORITY);
                    t.interrupt();
                }
            });
        }
    }

    public String getString() {
        return string;
    }

    public void setString(String string) {
        this.string = string;
    }

    public void show() {
        init();
        super.show();
    }
    
    public void show(Thread t) {
        this.t = t;
        init();
        super.show();
    }
}
